#!/usr/bin/env python3

import os
import sys
import tempfile

grub_exe_file=os.path.abspath(sys.argv[1])
r=None
try:
	r=open(grub_exe_file,"rb")
	grub_exe_data=r.read()
	str_head=b"pxe detect\nconfigfile\ndefault "
	i=grub_exe_data.find(str_head)
except:raise
finally:
	if r:r.close()
#print grub_exe_data[i:]
s=grub_exe_data[i:].replace(b'--ignore-cd',b'           ')

w = None
yes = False
fn = None
try:
	w0,fn=tempfile.mkstemp(prefix=grub_exe_file)
	os.close(w0)
	w=open(fn,"wb")
	w.write(grub_exe_data[:i])
	w.write(s)
	yes = True
except:raise
finally:
	if w:w.close()

if yes and fn:
	os.rename(fn,grub_exe_file)
else:
	os.remove(grub_exe_file)
